<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Product.php");						//--> Merchant
include_once("Operation.php");						//--> Operation
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation();						//--> Operation
$objProduct		= new Product(DB_TAG_SYSTEM, $uid);		//--> Product


$error_message 	= '';


$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);

if(isset($_GET['backurl_base64']))
	$backurl = base64_decode($_GET['backurl_base64']);


if(isset($_GET['action']))
	$action 	= $_GET['action'];
	

if($action == 'delete'){
	$relatedid = $_GET['relatedid'];
	
	$objProduct->update(DB_TAG_PUBLIC, "DELETE FROM products_related WHERE id = $relatedid");
}

if(isset($_POST['Submit'])){
	$productID 		= $_POST['productID'];
	$relatedClassID = $_POST['relatedClassID'];
	
	
	$objProduct->update(DB_TAG_PUBLIC, "INSERT INTO products_related SET type = $relatedClassID,  productID = $id, relatedProdID = $productID ON DUPLICATE KEY UPDATE relatedProdID = VALUES(relatedProdID)");
}

$sql 				= "SELECT id, name FROM products_related_class";
$arrRelated_class 	= $objProduct->getHash(DB_TAG_PUBLIC, $sql);

$sql 		= "SELECT pr.id, pr.relatedProdID, pd.name FROM products_related pr, products_description pd WHERE pr.type = 1 AND pr.relatedProdID = pd.productID AND pr.productID = $id";
$arrRelated = $objProduct->getAllData(DB_TAG_PUBLIC, $sql);

$arrProducts = array();

$where	= ' AND p.status = "normal"';
$order 	= ' ORDER BY pd.name';
$arrDataList = $objProduct->lists($arrOperate, $LANGEUAGE_ID, $where, $order, 0, 10000, '');
foreach($arrDataList as $arrRow){
	$arrProducts[$arrRow['id']] = $arrRow['name'] . " ($CURRENCY" . ($arrRow['price'] / 100) . ')';
}


/*----- out html -----*/
$smarty = new Smarty();	//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id',$id);

$smarty->assign('backurl',$backurl);
$smarty->assign('backurl_base64',base64_encode($backurl));

$smarty->assign('arrRelated_class', $arrRelated_class);
$smarty->assign('arrRelated', $arrRelated);

$smarty->assign('arrProducts', $arrProducts);

$smarty->display('listProduct_Related.htm');
?>
